﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChanSharp;

namespace ChanSharpTest
{
    class Program
    {
        static Thread thread;

        public static void Main(string[] args)
        {
            thread = new ChanSharp.Thread("http://arch.413chan.net/co21978498.html");

            thread.DownloadPosts();

            foreach (Post p in thread.Posts)
            {
                PrintPost(p);
            }
            Console.ReadLine();
        }

        static string[] placeholderImage = new string[]   {" ░░░░░░░░░░░ ",
                                                           " ░░░░░░░░░░░ ",
                                                           " ░I░M░A░G░E░ ",
                                                           " ░░░░░░░░░░░ ",
                                                           " ░░░░░░░░░░░ "}; //13 chars long

        public static void PrintPost(Post post)
        {
            Console.ForegroundColor = ConsoleColor.Gray;

            if (post.Subject != "")
            {
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write(post.Subject + " ");
            }
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write(post.Name + " ");
            if (post.Tripcode != "")
            {
                Console.Write(post.Tripcode + " ");
            }
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine(post.TimeFormattedAsChan + " No." + post.ID);

            //wrap the comment
            string[] message = post.Comment.WordWrap(80 - 14).Split('\n');

            //if there is a file
            if (post.File != null)
            {
                //display the stats
                Console.WriteLine(post.File.OriginalFilename + " (" + post.File.Filesize + ", WxH)");
                Console.WriteLine();

                int line;
                for (line = 0; line < message.Length; line++)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    if (line < placeholderImage.Length)
                    {
                        Console.Write(placeholderImage[line]);
                    }
                    else
                    {
                        Console.Write("             ");
                    }
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine(message[line]);
                }
                for (; line < placeholderImage.Length; line++)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(placeholderImage[line]);
                }


            }
            else
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Gray;
                for (int line = 0; line < message.Length; line++)
                {
                    Console.WriteLine("             " + message[line]);
                }

            }
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.Write("--------------------------------------------------------------------------------");
        }

    }
}
